package com.mysql.mybatis.controller;

import com.mysql.mybatis.dao.model.User;
import com.mysql.mybatis.service.JdbcTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * JdbcTemplate控制层
 *
 * @author : lhb
 * @date : 2019/11/5 10:55
 * @description : 访问地址：http://localhost:18888/swagger-ui.html
 */
@RestController
@RequestMapping(value = "/user/JdbcTemplate")
@Slf4j
@Api(description = "JdbcTemplate访问数据库")
public class JdbcTemplateController {

    @Autowired
    JdbcTemplateService jdbcTemplateService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "新增用户", response = ResponseEntity.class)
    public ResponseEntity save(User user) {
        jdbcTemplateService.save(user);
        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

    @PostMapping(value = "/getById")
    @ApiOperation(value = "根据id查询用户列表", response = ResponseEntity.class)
    public ResponseEntity getById(@RequestParam(value = "id") Long id) {
        return ResponseEntity.status(HttpStatus.OK).body(jdbcTemplateService.getById(id));
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新用户", response = ResponseEntity.class)
    public ResponseEntity update(User user) {
        jdbcTemplateService.update(user);
        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

    @PostMapping(value = "/deleteById")
    @ApiOperation(value = "删除用户", response = ResponseEntity.class)
    public ResponseEntity update(@RequestParam(value = "id") Long id) {
        jdbcTemplateService.deleteById(id);
        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

    @PostMapping(value = "/getAll")
    @ApiOperation(value = "查询所有的用户", response = ResponseEntity.class)
    public ResponseEntity getAll() {
        return ResponseEntity.status(HttpStatus.OK).body(jdbcTemplateService.getAll());
    }

    @GetMapping(value = "/getSeq")
    @ApiOperation(value = "获取全局索引", response = ResponseEntity.class)
    public ResponseEntity getSeq() {
        return ResponseEntity.status(HttpStatus.OK).body(jdbcTemplateService.getSeq());
    }

}
